using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Phone.Shell;
using SpaceShooter.Utilities;

namespace SpaceShooter.StateManagement
{
    public static class StateManager
    {
        /// <summary>
        /// Call this method to launch the menu
        /// </summary>
        /// <param name="g">The current game object</param>       
        public static void launchMenu(Game g)
        {
            clearGame(g);
            Components.inputManagerComponent i = new Components.inputManagerComponent(g);
            Components.menuScreenComponent m = new Components.menuScreenComponent(g);
            g.Components.Add(i);
            g.Components.Add(m);

           PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Enabled;
        }

        public static void launchGame(Game g, Match savedMatch)
        {
            clearGame(g);
            GameState gameState = new GameState();
            g.Services.AddService(typeof(GameState), gameState);
            Components.BulletsManagerComponent bmc = new Components.BulletsManagerComponent(g);
            Components.AsteroidsManagerComponent amc = new Components.AsteroidsManagerComponent(g);
            Components.RenderManager rm = new Components.RenderManager(g);
            Components.EnemyManagerComponent em = new Components.EnemyManagerComponent(g);
            Components.EffectManagerComponent fxm = new Components.EffectManagerComponent(g);
            Components.PowerUpManager pum = new Components.PowerUpManager(g);

            Components.LevelManagerComponent lm;
            {
                bool _restored = false;
                String _levelname = "";
                if (savedMatch != null)
                {
                    _restored = true;
                    _levelname = "Level-0.txt";// oltre a questo prendi il nome del livello
                }
                else
                    _levelname = "Level-0.txt";
                lm = new Components.LevelManagerComponent(g, _levelname, _restored); //MODIFICA IL NOME DEL LIVELLO RESTORATO
            }

            Components.inputManagerComponent im = new Components.inputManagerComponent(g);
            Components.PlayerComponent pc = new Components.PlayerComponent(g);
            Components.CollisionDetectorComponent cd = new Components.CollisionDetectorComponent(g);

            //Eventi
            lm.onNewEnemy += new Action<int, EnemyType>(em.OnNewEnemy);
            lm.onNewPoweUpEnemy += new Action<int, PowerUpType, EnemyType>(em.OnNewPowerUpEnemy);
            lm.onLevelEnd += new Action(rm.OnLevelEnd);
            lm.onLevelIsNotYetFinished += new Action(rm.OnLeveIsNotYetFinished);
            lm.onLevelStart += new Action<List<string[]>, bool>(rm.OnLevelStart);
            lm.onNewAsteroid += new Action<int, int, int>(amc.onNewAsteroidHandler);
            amc.onNewRock += new Action<Vector2, Vector2>(fxm.OnNewRock);
            em.onNewSmoke += new Action<Vector2>(fxm.OnNewSmoke);
            em.onNewExplosion += new Action<Vector2>(fxm.OnNewExplosion);
            em.onNewPowerUp += new Action<Vector2, PowerUpType>(pum.OnNewPowerUp);
            im.onTap += new Action<Vector2>(pc.input_onTap);
            im.onFlick += new Action<Vector2>(pc.input_onFlick);

            g.Components.Add(bmc);
            g.Components.Add(amc);
            g.Components.Add(im);
            g.Components.Add(pc);
            g.Components.Add(em);
            g.Components.Add(fxm);
            g.Components.Add(pum);
            g.Components.Add(rm);
            g.Components.Add(lm);
            g.Components.Add(cd);

            if (savedMatch != null)
                savedMatch.Restore(gameState);

            PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled;

            ((SpaceShooter)g).State = ApplicationState.GAME;
        }

        /// <summary>
        /// Call this method to clear Game Services and Game Components.
        /// </summary>
        /// <param name="g">The running Game object</param>
        public static void clearGame(Game g)
        {
            for (int counter = 0; counter < g.Components.Count; counter++)
            {
                (g.Components[counter] as GameComponent).Dispose();
                counter--;
            }
            g.Services.RemoveService(typeof(GameState));
        }
    }
}
